<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: ForgottenPassword.php 362 2010-02-03 08:41:11Z steen.rabol@gmail.com $
 *
 */

class ForgottenPassword extends PortalSystemPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->Title = Prado::localize("Forgotten password");
		if(!$this->Page->IsPostBack)
		{
			$mailsend = TPropertyValue::ensureInteger($this->Request['MailSend']);
			if($mailsend == 1)
			{
				$this->pnlForgottenPassword->Visible			= false;
				$this->pnlForgottenPassword->Enabled			= false;
				$this->pnlForgottenPasswordMailSend->Visible	= true;
				$this->pnlForgottenPasswordMailSend->Enabled	= true;
			}
		}
	}

	public function checkEmail($sender,$param)
	{
		$email	= strtolower($this->Email->Text);
		$id		= $this->DataAccess->createCommand("select * from tblusers where email='$email'")->queryRow();

		if($id)
		{
			$param->IsValid = true;
		}
		else
		{
			$param->IsValid = false;
		}
	}

	public function SendPassword($sender,$param)
	{
		if($this->IsValid)
		{
			$email		= strtolower($this->Email->Text);
			$urecord	= $this->DataAccess->createCommand("select * from tblusers where email='$email'")->queryRow();
			$site		= $this->Application->Parameters['SiteTitle'];
			$pasword	= PortalUtil::MakePassword();
			$username	= $urecord['name'];

			Prado::using('Application.Common.XMimeMail');
			$xMail = new XMimeMail();
			$xMail->setCharset('iso-8859-1');
			$xMail->setHtmlBody("Here is your infomation on $site<br>Username: $username<br>Password: $pasword",'');
 			$params['html_encoding'] = 'quoted-printable';
			$params['text_encoding'] = 'quoted-printable';
			$params['text_wrap'] = 998;
			$xMail->prepare($params);
			try
			{
				$xMail->send($urecord['full_name'], $urecord['email'], $site, $this->Application->Parameters['AdminEmail'], "Your information on $site");
				// Update new password in database
				$sql = "update tblusers set password='" . hash("sha256", $pasword) . "' where id=" . $urecord['id'];
				$this->DataAccess->createCommand($sql)->execute();
			}
			catch(Exception $e) { }
			$this->MailSend->Value = 1;
			$this->Response->redirect($this->Service->constructUrl('User.ForgottenPassword',array("MailSend"=>1),false));
		}
	}

    public function regenerateToken($sender,$param)
    {
        $this->Captcha->regenerateToken();
    }
}
?>